<template>
  <div class="login container">
    <Header></Header>
    <section>
      <div class="login-tel">
        <input
          type="text"
          placeholder="请输入手机号"
          pattern="[0-9*]"
          v-model.trim="userTel"
        />
      </div>
      <div class="login-tel">
        <input type="text" placeholder="请输入密码" v-model.trim="userPwd" />
      </div>
      <div class="login-btn" @click="userLogin">登 录</div>
      <div class="tab">
        <span @click="goLogin">短信登录</span>
        <span @click="goRecovery">找回密码</span>
        <span @click="goRegister">快速注册</span>
      </div>
    </section>
    <Tabbar></Tabbar>
  </div>
</template>
  
  <script>
import Tabbar from "@/components/common/Tabbar.vue";
import Header from "@/components/common/Header.vue";
import { Toast } from "mint-ui";
//引入封装后的axiox
import http from "@/common/api/request";

import { mapMutations } from "vuex";
export default {
  components: { Tabbar, Header },
  name: "Login",
  data() {
    return {
      userTel: "",
      userPwd: "",
      //验证规则
      rules: {
        //手机号验证
        userTel: {
          rule: /^1[23456789]\d{9}$/,
          msg: "手机号不能为空，有11位数字",
        },
        //密码验证
        userPwd: {
          rule: /^\w{6,12}$/,
          msg: "密码不能为空，要求有6~12位",
        },
      },
    };
  },
  methods: {
    ...mapMutations(["USER_LOGIN"]),
    //跳转到注册
    goRegister() {
      this.$router.push("/register");
    },
    //跳转到手机验证码登录
    goLogin() {
      this.$router.push("/login");
    },
    //跳转到找回密码
    goRecovery() {
      this.$router.push("/recovery");
    },
    //点击登录
    userLogin() {
      //前端验证
      if (!this.validata("userTel")) return;
      if (!this.validata("userPwd")) return;
      //发送请求，后端验证
      http
        .$axios({
          url: "/api/login",
          method: "Post",
          data: {
            userTel: this.userTel,
            userPwd: this.userPwd,
          },
        })
        .then((res) => {
          if (!res.success) {
            //密码错误
            Toast(res.msg);
            return;
          }
          Toast(res.msg);
          //登录成功，跳转页面,储存用户信息
          this.USER_LOGIN(res.data);
          this.$router.push("/my");
        });
    },
    //验证信息提示
    validata(key) {
      let bool = true;
      if (!this.rules[key].rule.test(this[key])) {
        Toast(this.rules[key].msg);
        bool = false;
      }
      return bool;
    },
  },
};
</script>
  
  <style scoped lang="scss">
section {
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: #f5f5f5;
  font-size: 0.32rem;
  div {
    margin: 0.266666rem 0;
    width: 8.933333rem;
    height: 1rem;
  }
  input {
    box-sizing: border-box;
    padding: 0 0.3rem;
    line-height: 1rem;
    background-color: #fff;
    border-radius: 8px;
  }
  .login-tel {
    margin-top: 0.4rem;
    input {
      width: 8.933333rem;
    }
  }
  .login-code {
    display: flex;
    justify-content: space-between;
    button {
      padding: 0 0.533333rem;
      line-height: 1rem;
      color: #fff;
      background-color: #b0352f;
      border: 0;
      border-radius: 4px;
    }
  }
  .login-btn {
    line-height: 1rem;
    background-color: #b0352f;
    border-radius: 6px;
    text-align: center;
    color: #fff;
    font-size: 0.48rem;
  }
  .tab {
    display: flex;
    justify-content: space-between;
    font-size: 0.373333rem;
  }
}
</style>